package cn.netjava.wk;

import java.io.File;
import java.util.LinkedList;

public class FileSearch extends Thread {
/*
 * @param:SearchFile
 * @value:the method has two values:directory,filename
 */

private LinkedList<String> list;	
private	String dir;
private	String filename;
private	StringEndOrNor string;
	
	
	FileSearch(String dir,String filename){		
		list=new LinkedList<String>();
		this.dir=dir;
		this.filename=filename;
	     if(filename.startsWith(".")){
		string=new StringEndWith();
	     }else {
	    	 string=new StringNorComp();
	     }
		//start();
	 }
	
	public void run(){
		if(!searchFile(dir,filename)){
			FileSearchThread.searchfailure++;
			}			
			}	
	
	
	public LinkedList<String> searchResoult(){
		 
		return list;
	}
	
	
	 public boolean searchFile(String dir,String filename){
		File pathfile=new File(dir);
		File[] file=pathfile.listFiles();
		if(file!=null){
		for(int i=0;i<file.length;i++){
			if(string.stringCompare(file[i].getName(),filename)){
				list.add(file[i].getAbsolutePath());
				System.out.println(file[i].getAbsolutePath());
				return true;
			}else{
				if(file[i].isDirectory()){
					searchFile(file[i].getAbsolutePath(),filename);
				}
			}
		}	
		}
	   
		return false;
	}
	
}
